package com.tanhua.manage.service;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.tanhua.domain.db.Announcement;
import com.tanhua.manage.domain.AnalysisByDay;
import com.tanhua.manage.exception.BusinessException;
import com.tanhua.manage.interceptor.AdminHolder;
import com.tanhua.manage.mapper.AnalysisByDayMapper;
import com.tanhua.manage.mapper.Logmapper;
import com.tanhua.manage.utils.ComputeUtil;
import com.tanhua.manage.vo.AnalysisSummaryVo;
import com.tanhua.manage.vo.AnalysisUsersVo;
import com.tanhua.manage.vo.DataPointVo;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AnalysisService {
    @Autowired
    private AnalysisByDayMapper analysisByDayMapper;
    @Autowired
    private Logmapper logmapper;

    public AnalysisSummaryVo summary() {
        Long userId = AdminHolder.getUserId();
        //今天日期
        String today = DateUtil.today();
        //昨天日期
        String yesterday = DateUtil.yesterday().toDateStr();
        //过去7天
        String last7Day = DateUtil.offsetDay(new DateTime(), -7).toDateStr();
        //过去30天
        String last30Day = DateUtil.offsetDay(new DateTime(), -30).toDateStr();
//                  ==========统计数据===========
        //今日数据
        AnalysisByDay todayData = analysisByDayMapper.findBydate(today);
        //昨日数据
        AnalysisByDay yesterdayData = analysisByDayMapper.findBydate(yesterday);
        //过去7天活跃数
        Long weekData = logmapper.findAfterday(last7Day);
        Long monthData = logmapper.findAfterday(last30Day);
        Long totalUsr = analysisByDayMapper.totalusercount();
        AnalysisSummaryVo vo = new AnalysisSummaryVo();
        vo.setCumulativeUsers(totalUsr);
        vo.setActivePassMonth(monthData);
        vo.setActivePassWeek(weekData);
        vo.setNewUsersToday(todayData.getNumRegistered());
        vo.setLoginTimesToday(todayData.getNumLogin());
        vo.setActiveUsersToday(todayData.getNumActive());
        vo.setNewUsersTodayRate(ComputeUtil.computeRate(todayData.getNumRegistered(), yesterdayData.getNumRegistered()));
        vo.setLoginTimesTodayRate(ComputeUtil.computeRate(todayData.getNumLogin(), yesterdayData.getNumLogin()));
        vo.setActiveUsersTodayRate(ComputeUtil.computeRate(todayData.getNumActive(), todayData.getNumActive()));
        return vo;
    }

    public AnalysisUsersVo getUsersCount(Long sd, Long ed, Integer type) {
        AnalysisUsersVo vo = new AnalysisUsersVo();
        String start = DateUtil.date(sd).toDateStr();
        String end = DateUtil.date(ed).toDateStr();
        String lastStart = DateUtil.offset(DateUtil.date(sd), DateField.YEAR, -1).toDateStr();
        String lastEnd = DateUtil.offset(DateUtil.date(ed), DateField.YEAR, -1).toDateStr();
        List<DataPointVo> thisYear =new ArrayList<>();
        List<DataPointVo> lastYear =new ArrayList<>();
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date startDate = dateFormat.parse(start);
            Date endDate = dateFormat.parse(end);
            Date lastStartDate = dateFormat.parse(lastStart);
            Date lastEndtDate = dateFormat.parse(lastEnd);

            switch (type) {
                case 101:
                    thisYear = analysisByDayMapper.countRegister(startDate, endDate);
                    lastYear = analysisByDayMapper.countRegister(lastStartDate, lastEndtDate);
                    break;
                case 102:
                    thisYear = analysisByDayMapper.countActive(startDate, endDate);
                    lastYear = analysisByDayMapper.countActive(lastStartDate, lastEndtDate);
                    break;
                case 103:
                    thisYear = analysisByDayMapper.countRetention(startDate, endDate);
                    lastYear = analysisByDayMapper.countRetention(lastStartDate, lastEndtDate);
                    break;
                default:
                    throw new BusinessException("数据查询有误");
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        vo.setThisYear(thisYear);
        vo.setLastYear(lastYear);
        return vo;
    }

    public void analysisData() {

    }
}
